<template>
  <div class="hwcreate-container">
    <div class="position">
      <span>当前位置：</span>
      <el-breadcrumb separator-class="el-icon-arrow-right">
        <el-breadcrumb-item>数据查询</el-breadcrumb-item>
        <el-breadcrumb-item :to="{ path: '/teacher/courseinfo' }"
          >已开设课程查询</el-breadcrumb-item
        >
        <el-breadcrumb-item>开设课程</el-breadcrumb-item>
      </el-breadcrumb>
    </div>
    <div class="hwform">
      <el-form
        :model="ruleForm"
        :rules="rules"
        ref="ruleForm"
        label-width="100px"
        class="demo-ruleForm"
      >
        <el-form-item label="课程ID" prop="courseid">
          <el-input v-model="ruleForm.courseid"></el-input>
        </el-form-item>
        <el-form-item label="课程名称" prop="coursename">
          <el-input v-model="ruleForm.coursename"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitForm('ruleForm')"
            >开设课程</el-button
          >
          <el-button @click="resetForm('ruleForm')">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import http from '@/api/teacherapi'
export default {
  name: "",
  data() {
    return {
      ruleForm: {
        courseid: "",
        coursename: "",
        date1: "",
        date2: "",
        desc: "",
      },
      rules: {
        courseid: [
          { required: true, message: "请输入课程id", trigger: "blur" },
          { min: 8, max: 8, message: "请输入8个数字", trigger: "blur" },
        ],
        coursename: [
          { required: true, message: "请选择课程名称", trigger: "blur" },
        ],
      },
      username:"",
    };
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
        http.coursecreate({courseid:this.ruleForm.courseid,coursename:this.ruleForm.coursename,teacher:this.username})
          .then((res)=>{
              if(res.status==200){
                  this.$Toast({
                      content:res.data.msg,
                      type:res.data.type
                  })
              }
          },()=>{
              return Promise.reject(new Error('获取服务器数据失败'))
          })
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
  },
  mounted() {
      this.username=localStorage.getItem("username")
  },
};
</script>

<style lang="less" scoped>
.position {
  height: 30px;
  line-height: 30px;
  .el-breadcrumb {
    display: inline-block;
  }
  & > span {
    font-size: 16px;
  }
}
.hwform {
  margin-top: 30px;
  .line {
    text-align: center;
  }
}
</style>
